package com.googlecode.volkien;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ToggleButton;

public class Main extends Activity {

	Messenger mService = null;
	boolean mBound = false;
	boolean mAwaitingServiceResponse = false;
	boolean mServiceEnabled;

	private ServiceConnection mConnection = new ServiceConnection() {

		public void onServiceConnected(ComponentName name, IBinder service) {
			mAwaitingServiceResponse = false;
			mService = new Messenger(service);
			mBound = true;
			registerVolumeClient();
			queryVolumeServiceStatus();
			updateUI();
		}

		public void onServiceDisconnected(ComponentName name) {
			mAwaitingServiceResponse = false;
			mBound = false;
			mService = null;
			bindVolumeService();
			updateUI();
		}

	};

	void bindVolumeService() {
		if (!mBound) {
			bindService(new Intent(VolumeService.ACTION_QUERY_STATUS, (Uri) null, Main.this, VolumeService.class), mConnection, Context.BIND_AUTO_CREATE);
			mAwaitingServiceResponse = true;
			updateUI();
		}
	}

	void unbindVolumeService() {
		if (mBound) {
			unbindService(mConnection);
			mBound = false;
			mService = null;
			mAwaitingServiceResponse = true;
			updateUI();
		}
	}

	class IncomingHandler extends Handler {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case VolumeService.MSG_SERVICE_STATUS:
				if (msg.arg1 == 0) {
					mAwaitingServiceResponse = false;
					mServiceEnabled = false;
					updateUI();
				}
				else {
					mAwaitingServiceResponse = false;
					mServiceEnabled = true;
					updateUI();
				}
				break;
			default:
				super.handleMessage(msg);
			}
		}
	}

	final Messenger mMessenger = new Messenger(new IncomingHandler());

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		final ToggleButton toggleVolumeControl = (ToggleButton) findViewById(R.id.toggle_volume_control);
		toggleVolumeControl.setEnabled(false);
		toggleVolumeControl.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				toggleServiceEnabled();
			}
		});

		bindVolumeService();

	}

	protected void toggleServiceEnabled() {
		mAwaitingServiceResponse = true;
		if (mServiceEnabled) {
			Message msg = Message.obtain(null, VolumeService.MSG_DISABLE_VOLUME_CONTROL);
			try {
				mService.send(msg);
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			stopService(new Intent(VolumeService.ACTION_ENABLE_VOLUME_CONTROL, (Uri) null, this, VolumeService.class));
		}
		else {
			startService(new Intent(VolumeService.ACTION_ENABLE_VOLUME_CONTROL, (Uri) null, this, VolumeService.class));
		}
		updateUI();
	}
	
	private void queryVolumeServiceStatus() {
		Message msg = Message.obtain(null, VolumeService.MSG_QUERY_STATUS);
		try {
			mService.send(msg);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void updateUI() {
		final ToggleButton button = (ToggleButton) findViewById(R.id.toggle_volume_control);
		button.setChecked(mServiceEnabled);
		button.setEnabled(mBound && !mAwaitingServiceResponse);
			
	}

	@Override
	protected void onDestroy() {
		unregisterVolumeClient();
		unbindVolumeService();
		super.onDestroy();
	}
	
	protected void registerVolumeClient() {
		Message msg = Message.obtain(null, VolumeService.MSG_REGISTER_CLIENT);
		msg.replyTo = mMessenger;
		try {
			mService.send(msg);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void unregisterVolumeClient() {
		Message msg = Message.obtain(null, VolumeService.MSG_UNREGISTER_CLIENT);
		msg.replyTo = mMessenger;
		try {
			mService.send(msg);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}